using System;
using System.Collections.Generic;
using Castle.Components.Binder;
using Castle.MonoRail.ActiveRecordSupport;
using Castle.MonoRail.Framework;
using IronMis2.Models;

namespace IronMis2.Controllers
{
    public class ExchangeController:BaseController
    {
        public void Index()
        {
            PropertyBag["exchanges"] = Exchange.FindAll();
        }
        public void Create()
        {
            PropertyBag["employees"] = Employee.FindAll();
            PropertyBag["commodities"] = Commodity.FindAll();
            PropertyBag["src"] = Storehouse.FindAll();
            PropertyBag["aim"] = Storehouse.FindAll();
        }
        public void Add([DataBind("exchange")] Exchange exchange)
        {
            exchange.CreateAndFlush();
            RedirectToAction("Index");
        }
        public void Edit(Guid id)
        {
            PropertyBag["exchange"] = Exchange.Find(id);

            PropertyBag["employees"] = Employee.FindAll();
            PropertyBag["commodities"] = Commodity.FindAll();
            PropertyBag["src"] = Storehouse.FindAll();
            PropertyBag["aim"] = Storehouse.FindAll();
        }
        public void Update([DataBind("exchange")] Exchange exchange)
        {
            exchange.UpdateAndFlush();
            RedirectToAction("Index");
        }
        public void Destory(Guid id)
        {
            Exchange.Find(id).DeleteAndFlush();
            RedirectToAction("Index");
        }

        public void ItemIndex(Guid id)
        {
            PropertyBag["exchange"] = Exchange.Find(id);
        }
        public CommodityRecord CreateFromCommodity(Commodity comodity)
        {
            CommodityRecord newCommodityRecord=new CommodityRecord();
            newCommodityRecord.CrcdId = comodity.ComId;
            newCommodityRecord.CrcdName = comodity.ComName;
            newCommodityRecord.CrcdDate = comodity.Comdate;
            newCommodityRecord.CrcdPrice = comodity.ComPrice;
            newCommodityRecord.CrcdRemark = comodity.ComRemark;
            newCommodityRecord.CrcdSimpleCode = comodity.ComBarCode;
            newCommodityRecord.CrcdSpec = comodity.ComSpec;
            newCommodityRecord.CrcdType = comodity.ComType;
            newCommodityRecord.CrcdStor = comodity.ComStor;
            newCommodityRecord.CrcdUnit = comodity.ComUnit;
            newCommodityRecord.Provider = comodity.Provider;
            newCommodityRecord.StoreHouse = comodity.Storhouse;
            return newCommodityRecord;
        }
        public void ItemCreate(Guid Id)
        {
            Exchange exchange = Exchange.Find(Id);

            Storehouse tempS = Storehouse.Find(exchange.ExSrc.Id);
            Commodity[] coms = Commodity.FindAllByProperty("Storhouse", tempS);

            PropertyBag["commodities"] = coms;
            Commodity[] selected=new Commodity[0];
            PropertyBag["selected"] = selected;
            PropertyBag["exchange"] = exchange;
        }
        public void ItemAdd([DataBind("exchange")] Exchange ex,[DataBind("selected")] Commodity[] selected)
        {
            Exchange exchange = Exchange.Find(ex.Id);
            foreach (Commodity com in selected)
            {
                CommodityRecord comRec = CreateFromCommodity(Commodity.Find(com.Id));
                comRec.CrcdExchangeNum = long.Parse(Params[com.Id.ToString()]);
                comRec.Exchange = exchange;
                comRec.CreateAndFlush();
            }
            PropertyBag["exchange"] = exchange;
            RenderView("ItemIndex");
        }

        public void ItemDestory(Guid id)
        {
            PropertyBag["exchange"] = CommodityRecord.Find(id).Exchange;
            CommodityRecord.Find(id).DeleteAndFlush();
            RenderView("ItemIndex");
        }
        public void ItemEdit(Guid id)
        {
            PropertyBag["commodityRecord"] = CommodityRecord.Find(id);
        }
        public void ItemUpdate([DataBind("commodityRecord")] CommodityRecord comRec)
        {
            CommodityRecord temp = CommodityRecord.Find(comRec.Id);
            PropertyBag["exchange"] = temp.Exchange;
            temp.CrcdExchangeNum = comRec.CrcdExchangeNum;
            temp.UpdateAndFlush();
            RenderView("ItemIndex");
        }
    }
}
